﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Borg.Framework
{
    public class HelpCommand : ICommand
    {
        private readonly Dictionary<string, ICommand> _commands;

        public HelpCommand(Dictionary<string, ICommand> commands)
        {
            _commands = commands;
        }

        public IEnumerable<string> GetArguments(string[] command, int pos)
        {
            return _commands.Keys;
        }

        private string HelpHandler(string[] command)
        {
            var builder = new StringBuilder();

            if (command.Length == 1)
                foreach (var str in _commands.Keys)
                    builder.AppendFormat("{0}{1}", str, Environment.NewLine);
            else if (_commands.ContainsKey(command[1]))
                builder.AppendLine(
                    _commands[command[1]].Help ?? "No help available.");
            else
                builder.AppendFormat("Command \"{0}\" not found.", command[1]);

            return builder.ToString();
        }

        public CommandHandler Handler
        {
            get { return HelpHandler; }
        }

        public string Help
        {
            get { return "Help for command \"help\""; }
        }

        public string Name
        {
            get { return "help"; }
        }
    }
}

